package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.db.AnalysisByDay;
import com.tanhua.domain.db.LastYear;
import com.tanhua.domain.db.ThisYear;
import com.tanhua.dubbo.api.db.AnalysisByDayApi;
import com.tanhua.dubbo.mapper.AnalysisByDayMapper;
import org.apache.dubbo.config.annotation.Service;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service
public class AnalysisByDayApiImpl implements AnalysisByDayApi {

    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;

    @Override
    public AnalysisByDay queryByRecodDate(Date recordDate) {

        QueryWrapper<AnalysisByDay> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AnalysisByDay::getRecordDate,recordDate);
        return analysisByDayMapper.selectOne(queryWrapper);
    }

    @Override
    public void updateById(AnalysisByDay analysisByDay) {
        analysisByDayMapper.updateById(analysisByDay);
    }

    @Override
    public void save(AnalysisByDay analysisByDay) {
        analysisByDayMapper.insert(analysisByDay);
    }

    @Override
    public Long queryCumulativeUsers() {
        return analysisByDayMapper.queryCumulativeUsers();

    }

    @Override
    public Long activePass(String passStr) {
        return analysisByDayMapper.activePass(passStr);
    }

    @Override
    public Long queryColumAndTime(String columName, String dayStr) {
        return analysisByDayMapper.queryColumAndTime(columName,dayStr);


    }

    /*
    管理系统，首页新增、活跃用户、次日留存率展示
    By Dzh
     */
    @Override
    public Map<String,List<Object>> queryThisYear(Long startDay, Long endDay, Integer type) {

        String typeStr =null;
        switch (type){
            case 101:
                typeStr="num_registered";
                break;
            case 102:
                typeStr="num_active";
                break;
            case 103:
                typeStr="num_retention1d";
                break;
        }
        String start = new DateTime(startDay).toString("yyyy-MM-dd");
        String end = new DateTime(endDay).toString("yyyy-MM-dd");
        String lastYearStart = new DateTime(start).minusYears(1).toString("yyyy-MM-dd");
        String lastYearEnd = new DateTime(end).minusYears(1).toString("yyyy-MM-dd");

        //查询今年
        QueryWrapper<AnalysisByDay> query =new QueryWrapper();
        query.lambda().between(AnalysisByDay::getRecordDate,start,end);
        List<AnalysisByDay> analysisThisYear = analysisByDayMapper.selectList(query);
        System.out.println(analysisThisYear);
        //查询去年
        QueryWrapper<AnalysisByDay> queryLast =new QueryWrapper();
        queryLast.lambda().between(AnalysisByDay::getRecordDate,lastYearStart,lastYearEnd);
        List<AnalysisByDay> analysisLastYear = analysisByDayMapper.selectList(queryLast);
        System.out.println(analysisLastYear);


        List<Object> thisYears =new ArrayList<>();
        List<Object> lastYears =new ArrayList<>();
        for (int i = 0; i < analysisThisYear.size(); i++) {
            LastYear lastYear =new LastYear();
            String date = new DateTime(analysisThisYear.get(i).getRecordDate()).toString("dd");
            lastYear.setTitle(date);
            ThisYear thisYear =new ThisYear();
            thisYear.setTitle(date);
            switch (typeStr){
                case"num_registered":
                    thisYear.setAmount(analysisThisYear.get(i).getNumRegistered());
                    lastYear.setAmount(analysisLastYear.get(i).getNumRegistered());
                    break;
                case"num_active":
                    thisYear.setAmount(analysisThisYear.get(i).getNumActive());
                    lastYear.setAmount(analysisLastYear.get(i).getNumActive());
                    break;
                case"num_retention1d":
                    thisYear.setAmount(analysisThisYear.get(i).getNumRetention1d());
                    lastYear.setAmount(analysisLastYear.get(i).getNumRetention1d());
                    break;
            }

            thisYears.add(thisYear);
            lastYears.add(lastYear);

        }
        Map<String,List<Object>> map=new HashMap<>();
        map.put("thisYear",thisYears);
        map.put("lastYear",lastYears);
        return map;
    }
}
